package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.CarNeedDataContract;
import com.shd.edanpei.presenter.CarNeedDataPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 车辆订单 订单所需材料2
 */

public class CarNeedDataActivity2 extends BaseActivity<CarNeedDataPresenter>
        implements CarNeedDataContract.CarNeedDataIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    //    @BindView(R.id.et_repayWay)
//    EditText etRepayWay;
    @BindView(R.id.tv_repayWay)
    TextView tvRepayWay;
    @BindView(R.id.tv_repayDate)
    TextView tvRepayDate;
    @BindView(R.id.radio_group_insurance)
    RadioGroup radioGroupInsurance;
    @BindView(R.id.policy_term)
    View policyTerm;
    @BindView(R.id.tv_policyStartTerm)
    TextView tvPolicyStart;
    @BindView(R.id.tv_policyEndTerm)
    TextView tvPolicyEnd;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_done)
    TextView tvDone;

    private int from;


    @Override
    protected int setLayout() {
        return R.layout.activity_carneeddata2;
    }

    @Override
    protected CarNeedDataPresenter bindPresenter() {
        return new CarNeedDataPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("订单所需材料");
        Bundle extras = getIntent().getExtras();
        CustomerBean orderBean = extras.getParcelable(Constant.DATA);
        from = extras.getInt(Constant.FROM);
        mPresenter.setCustomerBean(orderBean);
        if (from == 1) {
            tvDone.setText("确认修改");
            headline.setText("修改订单所需材料");
        }
    }

    @Override
    protected void initListener() {
        radioGroupInsurance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                boolean isShow = checkedId == R.id.radio_button_yes;
                mPresenter.setInsurance(isShow);
                policyTerm.setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        });
        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setRemark(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
//        etRepayWay.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                mPresenter.setRepayWay(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBank() {
        finish();
    }

    /**
     * 选择订单金额
     */
    @OnClick(R.id.rl_amount)
    void onAmount() {
        mPresenter.setAmount(tvAmount);
    }

    /**
     * 预期比
     */
    @OnClick(R.id.rl_rate)
    void onRate() {
        mPresenter.setRate(tvRate);
    }

    /**
     * 归还方式
     */
    @OnClick(R.id.ll_repayWay)
    void OnRepayWay() {
        mPresenter.setRepayWay(tvRepayWay);
    }

    /**
     * 订单期限
     */
    @OnClick(R.id.rl_repayDate)
    void onRepayDate() {
        mPresenter.setRepayDate(tvRepayDate);
    }

    @OnClick(R.id.tv_policyStartTerm)
    void choosePolicyStartTerm() {
        mPresenter.choosePolicyStartTerm(tvPolicyStart);
    }

    @OnClick(R.id.tv_policyEndTerm)
    void choosePolicyEndTerm() {
        mPresenter.choosePolicyEndTerm(tvPolicyEnd);
    }


    @OnClick(R.id.tv_done)
    void onDone() {
        if (from == 1) {
            mPresenter.update();
        } else {
            mPresenter.done();
        }
    }


    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(getResources().getColor(R.color.black));
    }

    @Override
    public void setData(CustomerBean bean) {
       if (!TextUtils.isEmpty(bean.getApplyAmount()))setText(tvAmount, bean.getApplyAmount());
       if (!TextUtils.isEmpty(bean.getApplyInterest()))setText(tvRate, bean.getApplyInterest());
       if (!TextUtils.isEmpty(bean.getApplyRepaymentType()))setText(tvRepayWay, bean.getApplyRepaymentType());
       if (!TextUtils.isEmpty(bean.getApplyTerm()))setText(tvRepayDate, bean.getApplyTerm());
        radioGroupInsurance.check(bean.isHaveCarInsurance() ? R.id.radio_button_yes : R.id.radio_button_no);
        policyTerm.setVisibility(bean.isHaveCarInsurance() ? View.VISIBLE : View.GONE);
        if (bean.isHaveCarInsurance()) {
            String[] term = bean.getInsuranceTerm().split("至");
            setText(tvPolicyStart, term[0]);
            mPresenter.setStartTerm(term[0]);
            setText(tvPolicyEnd, term[1]);
            mPresenter.setEndTerm(term[1]);
        }
        if (!TextUtils.isEmpty(bean.getRemark()))  setText(etRemark, bean.getRemark());
    }


}
